﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    public sealed partial class PauseOptionsState : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;

        Menu menu;
        bool changeMade;

        private void buttonBack()
        {
            StateManager.currentState = States.PAUSE;
            if (changeMade)
            {
                changeMade = false;
                GameVariables.writeConfig();
            }
        }

        private void changeFadingParticles()
        {
            GameVariables.fadingParticles = !GameVariables.fadingParticles;
            menu = makeMenu();
            changeMade = true;
        }

        private void changeSoundEffects()
        {
            GameVariables.soundEffects = !GameVariables.soundEffects;
            menu = makeMenu();
            changeMade = true;
        }

        private void changeMusic()
        {
            GameVariables.music = !GameVariables.music;
            if (GameVariables.music)
            {
                MediaPlayer.Play(ResourceManager.getSong("ingameSong"));
            }
            else
            {
                MediaPlayer.Stop();
            }
            menu = makeMenu();
            changeMade = true;
        }

        public PauseOptionsState(Game game, SpriteBatch spriteBatch)
            : base(game)
        {
            this.spriteBatch = spriteBatch;
            changeMade = false;
            menu = makeMenu();
        }

        private Menu makeMenu()
        {
            ButtonContainer buttons = new ButtonContainer();
            if (GameVariables.fadingParticles)
            {
                buttons.AddButton(ResourceManager.getTexture("buttonsOptionsFPON"),
                                new ButtonEvent(changeFadingParticles),
                                Color.Green,
                                Color.Orange,
                                Color.CornflowerBlue);
            }
            else
            {
                buttons.AddButton(ResourceManager.getTexture("buttonsOptionsFPOFF"),
                                new ButtonEvent(changeFadingParticles),
                                Color.Red,
                                Color.Orange,
                                Color.CornflowerBlue);
            }

            if (GameVariables.soundEffects)
            {
                buttons.AddButton(ResourceManager.getTexture("buttonsOptionsSEON"),
                                new ButtonEvent(changeSoundEffects),
                                Color.Green,
                                Color.Orange,
                                Color.CornflowerBlue);
            }
            else
            {
                buttons.AddButton(ResourceManager.getTexture("buttonsOptionsSEOFF"),
                                new ButtonEvent(changeSoundEffects),
                                Color.Red,
                                Color.Orange,
                                Color.CornflowerBlue);
            }

            if (GameVariables.music)
            {
                buttons.AddButton(ResourceManager.getTexture("buttonsOptionsMusicON"),
                                new ButtonEvent(changeMusic),
                                Color.Green,
                                Color.Orange,
                                Color.CornflowerBlue);
            }
            else
            {
                buttons.AddButton(ResourceManager.getTexture("buttonsOptionsMusicOFF"),
                                new ButtonEvent(changeMusic),
                                Color.Red,
                                Color.Orange,
                                Color.CornflowerBlue);
            }

            buttons.AddButton(ResourceManager.getTexture("buttonBack"),
                                new ButtonEvent(buttonBack),
                                Color.White,
                                Color.Orange,
                                Color.Red);

            return new Menu(Game, buttons, 297, 52);
        }

        public sealed override void Update(GameTime gameTime)
        {
            if (StateManager.currentState == States.PAUSEOPTIONS)
            {
                menu.Update(gameTime);
            }
        }

        public sealed override void Draw(GameTime gameTime)
        {
            if (StateManager.currentState == States.PAUSEOPTIONS)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(ResourceManager.getTexture("Loading"), Game.GraphicsDevice.Viewport.TitleSafeArea, Color.White);
                spriteBatch.DrawString(ResourceManager.getFont("JingJing"), "Pause...", new Vector2(50, Game.GraphicsDevice.Viewport.Height - 120), Color.Orange);
                spriteBatch.End();
                menu.Draw(spriteBatch);
            }
        }
    }
}